package com.kang.basicalgorithm.selectionsort;

import java.util.Arrays;

import static com.kang.basicalgorithm.bubblesort.BubbleSort.swap;

/**
 * @author xukang
 * @date 2021-10-22 17:12
 */
public class SelectionSort {
    public static void main(String[] args) {
        int[] array = {5, 3, 7, 2, 1, 9, 8, 4};
        selectSort(array);
    }
    private static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            //i代表的每轮选择最小元素要交换到的目标索引
            //代表最小的元素索引
            int s = i;
            for (int j = s + 1; j < array.length; j++) {
                if (array[s] > array[j]) {
                    s = j;
                }
            }
            if (s != i) {
                swap(array, s, i);
            }
            System.out.println(Arrays.toString(array));
        }
    }
}
